﻿#include "precomp.h"
#include "jthreadpool_global.h"
#include <QCoreApplication>
#include <stdlib.h>
#include <stdio.h>

/**
 * @brief jMalloc
 * @param size
 * @return
 */
void *jMalloc(unsigned long size)
{
    return qMalloc(size);
}

/**
 * @brief jFree
 * @param ptr
 */
void jFree(void *ptr)
{
    qFree(ptr);
}

/**
 * @brief jRealloc
 * @param ptr
 * @param size
 * @return
 */
void *jRealloc(void *ptr, unsigned long size)
{
    return qRealloc(ptr, size);
}

/**
 * @brief jMallocAligned
 * @param size
 * @param alignment
 * @return
 */
void *jMallocAligned(unsigned long size, unsigned long alignment)
{
    return qMallocAligned(size, alignment);
}

/**
 * @brief jReallocAligned
 * @param ptr
 * @param size
 * @param oldsize
 * @param alignment
 * @return
 */
void *jReallocAligned(void *ptr, unsigned long size, unsigned long oldsize, unsigned long alignment)
{
    return qReallocAligned(ptr, size, oldsize, alignment);
}

/**
 * @brief jFreeAligned
 * @param ptr
 */
void jFreeAligned(void *ptr)
{
    qFreeAligned(ptr);
}

/**
 * @brief jMemCopy
 * @param dest
 * @param src
 * @param n
 * @return
 */
void *jMemCopy(void *dest, const void *src, unsigned long n)
{
    return qMemCopy(dest, src, n);
}

/**
 * @brief jMemSet
 * @param dest
 * @param c
 * @param n
 * @return
 */
void *jMemSet(void *dest, int c, unsigned long n)
{
    return qMemSet(dest, c, n);
}

// - class JThreadPoolCorePrivate -

class JThreadPoolCorePrivate
{
    friend class JThreadPoolCore;
public:
    JThreadPoolCorePrivate()
    {
        if (qApp == 0) {
            int argc = 0;
            (void)new QCoreApplication(argc, 0);
        }
    }

    ~JThreadPoolCorePrivate()
    {
        delete qApp;
    }

private:
    static JThreadPoolCore *_inst;
};

/**
 * @brief JThreadPoolCorePrivate::_inst
 */
JThreadPoolCore *JThreadPoolCorePrivate::_inst = 0;

// - class JThreadPoolCore -

/**
 * @brief JThreadPoolCore::instance
 * @return
 */
JThreadPoolCore *JThreadPoolCore::instance()
{
    if (JThreadPoolCorePrivate::_inst == 0) {
        JThreadPoolCorePrivate::_inst = new JThreadPoolCore();
    }

    return JThreadPoolCorePrivate::_inst;
}

/**
 * @brief JThreadPoolCore::releaseInstance
 */
void JThreadPoolCore::releaseInstance()
{
    if (JThreadPoolCorePrivate::_inst != 0) {
        delete JThreadPoolCorePrivate::_inst;
        JThreadPoolCorePrivate::_inst = 0;
    }
}

/**
 * @brief JThreadPoolCore::run
 * @return
 */
int JThreadPoolCore::run()
{
    if (qApp) {
        //
    }

    return 0;
}

/**
 * @brief JThreadPoolCore::exec
 * @return
 */
int JThreadPoolCore::exec()
{
    if (!qApp) {
        return -1;
    }

    return qApp->exec();
}

/**
 * @brief JThreadPoolCore::JThreadPoolCore
 */
JThreadPoolCore::JThreadPoolCore()
{
    d = new JThreadPoolCorePrivate();
}

/**
 * @brief JThreadPoolCore::~JThreadPoolCore
 */
JThreadPoolCore::~JThreadPoolCore()
{
    delete d;
}
